package com.lwl.dubbo.serviceImpl;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.Resource;

import java.util.Set;

import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lwl.dubbo.dao.UserMapper;
import com.lwl.dubbo.service.UserService;
import com.lwl.dubbo.bean.User;
import com.lwl.dubbo.util.MD5Helper;

/**
 * 用户service
 * @author 王凯
 *
 */
@Service("userService")
public class UserServiceImpl implements UserService{

	/**
	 * 用户Mapper
	 */
	@Resource(name="userMapper")
	private UserMapper userMapper;
	/*@Autowired
	private UserMapper userMapper;*/
	
	/**
	 * 创建SqlSession的工厂
	 */
	@SuppressWarnings("unused")
	@Autowired
	private SqlSessionFactory sqlSessionFactory;
	
	/**
	 * 修改用户
	 * @param user
	 * @return
	 */
	@Transactional
	public boolean updateUser(User user) {
		
		return userMapper.updateByPrimaryKeySelective(user) > 0;
	}
	
	/**
	 * 逐个添加用户
	 * @param user
	 * @return
	 */
	@Transactional
	public boolean addUser(User user) {
		
		return userMapper.insertSelective(user) > 0;
	}
	
	/**
	 * 找出未被禁用的用户（作为备选项）
	 * @return
	 */
	public List<User> findValidUser() {
		
		User user = new User();
		user.setUserstate(1);
		
		return userMapper.selectByKeyWord(user);
	}
	
	/**
	 * 检查登录名是否重复
	 * @param userid 用户id
	 * @param loginname 登录名
	 * @return true 表示重复， false 表示不重复
	 */
	public boolean hasSameName(Integer userid, String loginname) {
		
		return userMapper.loginnameValidate(userid, loginname).size() > 0;
	}
	
	/**
	 * 登录检查
	 * @param loginname 登录名
	 * @param password 密码
	 * @return true 表示登录检查通过, false 表示不通过
	 */
	public User loginValidate(String loginname, String password) {
		
		return userMapper.loginValidate(loginname, password);
	}
	
	/**
	 * 模糊查询
	 * @return
	 */
	public List<User> findByKeyword(User user) {
		
		return userMapper.selectByKeyWord(user);
	}
	
	/**
	 * 按用户id查询
	 * @param userid
	 * @return
	 */
	public User findbyId(Integer userid) {
		return userMapper.selectByPrimaryKey(userid);
	}

	@Override
	public boolean addUserBatch(InputStream inputStream) {
		// TODO Auto-generated method stub
		return false;
	}
}
